package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysPhase;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.PhaseService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.PhaseVo;
import com.github.pagehelper.PageInfo;

/**
 * 学段管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/phase", name = "学段管理")
public class PhaseController {
	
	@Autowired
	private PhaseService	phaseService;
	
	/**
	 *
	 * @apiGroup phase
	 * @api {get} /phase/list 获取学段列表
	 * @apiVersion 1.0.0
	 * @apiName getPhaseList
	 *
	 * 
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "phaseId": 1,
		            "phaseName": "小学",
		            "duration": 6,
		            "sortNum": 1
		        },
		        {
		            "phaseId": 2,
		            "phaseName": "初中",
		            "duration": 3,
		            "sortNum": 2
		        },
		        {
		            "phaseId": 3,
		            "phaseName": "高中",
		            "duration": 3,
		            "sortNum": 3
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "学段列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/phase/list')")
    public RestResult getPhaseList() throws Exception {

        List<PhaseVo> phaseList = phaseService.getPhaseList();
        return RestResult.ok(phaseList);
    }
    
    /**
	 *
	 * @apiGroup phase
	 * @api {get} /phase/listPage 获取学段列表(分页)
	 * @apiVersion 1.0.0
	 * @apiName getPhaseListPage
	 *
	 * @apiParam {Integer} pageNum 页码
	 * @apiParam {Integer} pageSize 每页行数
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "total": 3,
		        "list": [
		            {
		                "phaseId": 1,
		                "phaseName": "小学",
		                "duration": 6,
		                "sortNum": 1
		            },
		            {
		                "phaseId": 2,
		                "phaseName": "初中",
		                "duration": 3,
		                "sortNum": 2
		            },
		            {
		                "phaseId": 3,
		                "phaseName": "高中",
		                "duration": 3,
		                "sortNum": 3
		            }
		        ],
		        "pageNum": 1,
		        "pageSize": 10,
		        "size": 3,
		        "startRow": 1,
		        "endRow": 3,
		        "pages": 1,
		        "prePage": 0,
		        "nextPage": 0,
		        "isFirstPage": true,
		        "isLastPage": true,
		        "hasPreviousPage": false,
		        "hasNextPage": false,
		        "navigatePages": 8,
		        "navigatepageNums": [
		            1
		        ],
		        "navigateFirstPage": 1,
		        "navigateLastPage": 1,
		        "firstPage": 1,
		        "lastPage": 1
		    }
		}
	 *
	 *
	 */
    @RequestMapping(value = "listPage", name = "学段列表(分页)", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/phase/listPage')")
    public RestResult getPhaseListPage(@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) throws Exception {

    	PageInfo<PhaseVo> pageInfo = phaseService.getPhaseListPage(pageNum,pageSize);
        return RestResult.ok(pageInfo);
    }
    
        
    /**
	 *
	 * @apiGroup phase
	 * @api {post} /phase/save 保存学段信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysPhase} phase 班级信息
	 * @apiParamExample 请求示例（json）
	 * {
		
			"phaseId": 2,
            "phaseName": "初中",
            "duration": 3,
            "sortNum": 2
		
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "phaseId": 2
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "save", name = "保存学段信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/phase/save')")
	public RestResult save(@Valid @RequestBody SysPhase phase) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = phaseService.savePhase(phase);
	
		return RestResult.ok(result);
	}
	
	/**
	 *
	 * @apiGroup phase
	 * @api {get} /phase/info 获取学段信息
	 * @apiVersion 1.0.0
	 * @apiName getPhase
	 *
	 * @apiParam {Long} phaseId  学段id
	 *  
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "phaseId": 2,
		        "phaseName": "初中",
		        "duration": 3,
		        "sortNum": 2
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "学段信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/phase/info')")
	public RestResult getPhase(@RequestParam Long phaseId) throws Exception{
		SysPhase phase = null;
		PhaseVo phaseVo = null;
		
		phase = phaseService.selectByPrimaryKey(phaseId);
		
		if (phase!=null)
		{
			phaseVo = new PhaseVo();
			BeanUtils.copyProperties(phase,phaseVo);
		}

		return RestResult.ok(phaseVo);
	}
	
	/**
	 *
	 * @apiGroup phase
	 * @api {post} /phase/delete 删除学段信息
	 * @apiVersion 1.0.0
	 * @apiName deletePhase
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "phaseId": 2
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/delete" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/phase/delete')")
    public RestResult deletePhase(@RequestBody Map<String,Object> dataMap) throws Exception {
   	   
      	Long phaseId = MapUtils.getLong(dataMap, "phaseId");
      	
      	if (phaseId == null || phaseId<=0)
      	{
      		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
      	}
    	    			
    	int isOk = phaseService.deletePhase(phaseId);
    	if (isOk>0)
		{
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
    }


}
